﻿using ContourSplit.Scaffold;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ContourSplit
{
	public class Config
	{
		public static Config config = new Config();
		string fileName = "config.xml";
		private double _WidthInPX;
		public Calibrator calibrator;
		public Config()
		{
			if (!Load())
			{
				_WidthInPX = 0;
			}
		}
		public Config(Config old)
		{
			var p = old.GetType().GetProperties();
			foreach (var item in p)
			{
				var oldVal = item.GetValue(old);
				this.GetType().GetProperty(item.Name).SetValue(this, oldVal);
			}
		}
		public bool Save()
		{
			XmlDocument doc = new XmlDocument();
			XmlNode root = doc.CreateElement("root");
			doc.AppendChild(root);
			var p = GetType().GetProperties();
			foreach (var item in p)
			{
				XmlElement n = doc.CreateElement(item.Name);
				if (item.PropertyType != typeof(Point))
					n.SetAttribute("value", item.GetValue(this).ToString());
				else
					n.SetAttribute("value", new PointConverter().ConvertToString(item.GetValue(this)));
				root.AppendChild(n);
			}
			try
			{
				doc.Save(fileName);
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine(e.StackTrace);
				return false;
			}
			return true;
		}
		public bool Load()
		{
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				var p = GetType().GetProperties();
				XmlNode root = doc.FirstChild;
				foreach (var item in p)
				{
					var t = root[item.Name];
					if (item.PropertyType == typeof(string))
						item.SetValue(this, t.GetAttribute("value"));
					else if (item.PropertyType == typeof(int))
						item.SetValue(this, Convert.ToInt32(t.GetAttribute("value")));
					else if (item.PropertyType == typeof(double))
						item.SetValue(this, Convert.ToDouble(t.GetAttribute("value")));
					else if (item.PropertyType == typeof(Point))
						item.SetValue(this, new PointConverter().ConvertFromString(t.GetAttribute("value")));
					else if (item.PropertyType == typeof(float))
						item.SetValue(this, float.Parse(t.GetAttribute("value")));
					else if (item.PropertyType == typeof(bool))
						item.SetValue(this, bool.Parse(t.GetAttribute("value")));
					else if (item.PropertyType == typeof(ushort))
						item.SetValue(this, ushort.Parse(t.GetAttribute("value")));
					else
						throw new Exception("\n\n!!! Not Implement Type Convertion When Reflect !!!\n\n");
				}
			}
			catch (FileNotFoundException)
			{
				return false;
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine(e.StackTrace);
				return false;
			}
			return true;
		}
		public double WidthInPX
		{
			get
			{
				if (_WidthInPX == 0)
					return 100;
				return _WidthInPX;
			}
			set
			{
				_WidthInPX = value;
			}
		}
		public int Threshold { get; set; }
		public float MCPerPX { get; set; }
		public float ZInitH { get; set; }
		public float ZActH { get; set; }
		public float MCPerDG { get; set; }
		public float MCPerMM { get; set; }
		public int VMin { get; set; }
		public int VMax { get; set; }
		public float Tacc { get; set; }

		public Point Offset { get; set; }
		public bool PaintOnFinally { get; set; }
		public int Passes { get; set; }
		public ushort GluePort { get; set; }
		public ushort CompletePort { get; set; }
		public int ChessHei { get; set; }
		public int ChessWid { get; set; }

	}
}
